package top.hundreds.oasys.controller.FrontController;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import top.hundreds.common.crud.bean.PageParam;
import top.hundreds.common.web.api.ApiResult;
import top.hundreds.oasys.bean.entity.Announcement;
import top.hundreds.oasys.bean.vo.AnnouncementVo;
import top.hundreds.oasys.service.AnnouncementService;
import top.hundreds.oasys.utils.MvcHttpUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Announcement)表控制层
 *
 * @author lcf
 * @since 2021-01-29 19:12:53
 */
@RestController
@Slf4j
@RequestMapping("/front/announcement")
public class AnnouncementController {
    /**
     * 服务对象
     */
    @Resource
    private AnnouncementService announcementService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/get/{id}")
    public ApiResult<AnnouncementVo> selectOne(@PathVariable Long id) {
        AnnouncementVo announcementVo = this.announcementService.buildVo(this.announcementService.get(id));
        return ApiResult.success(announcementVo);
    }

    /**
     * 获取分页
     *
     * @param pageParam 分页参数
     * @return 通知分页
     */
    @PostMapping("/list")
    public ApiResult<List<AnnouncementVo>> list(@RequestBody PageParam<Announcement> pageParam) {
        List<AnnouncementVo> list = this.announcementService.getList(pageParam);
        return ApiResult.success(list);
    }

    /**
     * 新增或更新通知
     * 有id为更新，无id为新增
     *
     * @param announcement 通知实体类
     * @return success
     */
    @PostMapping("/save")
    public ApiResult<String> save(@RequestBody Announcement announcement) {
        if (announcement.getId() == null) {
            announcement.setUserId(MvcHttpUtil.getCurrUserId());
            // 新增操作
            this.announcementService.insert(announcement);
        } else {
            // 更新操作
            this.announcementService.update(announcement);
        }
        return ApiResult.success();
    }

    /**
     * 删除通知
     *
     * @param id 通知id
     * @return success
     */
    @DeleteMapping("/remove/{id}")
    public ApiResult<String> remove(@PathVariable Long id) {
        if (id == null) {
            return ApiResult.fail("请传入参数");
        }
        // 删除操作
        this.announcementService.delete(id);
        return ApiResult.success();
    }

    /**
     * 发布/撤回通知
     *
     * @param id 通知id
     * @param status 状态值
     * @return success
     */
    @GetMapping("/changeStatus/{id}/{status}")
    public ApiResult<String> issue(@PathVariable Long id,@PathVariable Integer status) {
        if (id == null) {
            return ApiResult.fail("请传入参数");
        }
        this.announcementService.setStatus(id, status);
        return ApiResult.success();
    }

}
